<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Prepares and executes a query</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.sqlsrv-query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sqlsrv_query</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">sqlsrv_query</span> &mdash; <span class="dc-title">Prepares and executes a query</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.sqlsrv-query-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sqlsrv_query</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$conn</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$sql</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$params</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
  ]] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Prepares and executes a query.
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.sqlsrv-query-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">conn</code></dt>

     <dd>

      <p class="para">
       A connection resource returned by <span class="function"><a href="sqlsrv_connect.html" class="function">sqlsrv_connect()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">sql</code></dt>

     <dd>

      <p class="para">
       The string that defines the query to be prepared and executed.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">params</code></dt>

     <dd>

      <p class="para">
       An array specifying parameter information when executing a parameterized query. 
       Array elements can be any of the following:
       <ul class="simplelist">
        <li class="member">A literal value</li>
        <li class="member">A PHP variable</li>
        <li class="member">An array with this structure: 
        array($value [, $direction [, $phpType [, $sqlType]]])</li>
       </ul>
       The following table describes the elements in the array structure above:
       </p>
       <table class="doctable table">
       <caption><strong>Array structure</strong></caption>
       
        <thead>
         <tr>
          <th>Element</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td>$value</td>
          <td>A literal value, a PHP variable, or a PHP by-reference variable.</td>
         </tr>

         <tr>
          <td>$direction (optional)</td>
          <td>One of the following SQLSRV constants used to indicate the 
          parameter direction: SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, SQLSRV_PARAM_INOUT. 
          The default value is SQLSRV_PARAM_IN.</td>
         </tr>

         <tr>
          <td>$phpType (optional)</td>
          <td>A SQLSRV_PHPTYPE_* constant that specifies PHP data type of the 
          returned value.</td>
         </tr>

         <tr>
          <td>$sqlType (optional)</td>
          <td>A SQLSRV_SQLTYPE_* constant that specifies the SQL Server data 
          type of the input value.</td>
         </tr>

        </tbody>
       
      </table>

     </dd>

    
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       An array specifing query property options. The supported keys are described 
       in the following table:
      </p>
      <table class="doctable table">
       <caption><strong>Query Options</strong></caption>
       
        <thead>
         <tr>
          <th>Key</th>
          <th>Values</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td>QueryTimeout</td>
          <td>A positive integer value.</td>
          <td>Sets the query timeout in seconds. By default, the driver will 
          wait indefinitely for results.</td>
         </tr>

         <tr>
          <td>SendStreamParamsAtExec</td>
          <td><strong><code>TRUE</code></strong> or <strong><code>FALSE</code></strong> (the default is <strong><code>TRUE</code></strong>)</td>
          <td>Configures the driver to send all stream data at execution (<strong><code>TRUE</code></strong>), 
          or to send stream data in chunks (<strong><code>FALSE</code></strong>). By default, the value is set 
          to <strong><code>TRUE</code></strong>. For more information, see <span class="function"><a href="sqlsrv_send_stream_data.html" class="function">sqlsrv_send_stream_data()</a></span>.</td>
         </tr>

         <tr>
          <td>Scrollable</td>
          <td>SQLSRV_CURSOR_FORWARD, SQLSRV_CURSOR_STATIC, SQLSRV_CURSOR_DYNAMIC, 
          or SQLSRV_CURSOR_KEYSET</td>
          <td>See <a href="http://msdn.microsoft.com/en-us/library/ee376927.aspx" class="link external">&raquo;&nbsp;Specifying 
          a Cursor Type and Selecting Rows</a> in the Microsoft SQLSRV documentation.</td>
         </tr>

        </tbody>
       
      </table>

     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.sqlsrv-query-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a statement resource on success and <strong><code>FALSE</code></strong> if an error occurred.
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.sqlsrv-query-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2588">
    <p><strong>Example #1 <span class="function"><strong>sqlsrv_query()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serverName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"serverName\sqlexpress"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$connectionInfo&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"username"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"password"&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$serverName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br />if(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;Table_1&nbsp;(id,&nbsp;data)&nbsp;VALUES&nbsp;(?,&nbsp;?)"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$params&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"some&nbsp;data"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_query</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$params</span><span style="color: #007700">);<br />if(&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.sqlsrv-query-notes">
  <h3 class="title">注释</h3>
  <p class="para">
   For statements that you plan to execute only once, use <span class="function"><strong>sqlsrv_query()</strong></span>. 
   If you intend to re-execute a statement with different parameter values, use 
   the combination of <span class="function"><a href="sqlsrv_prepare.html" class="function">sqlsrv_prepare()</a></span> and <span class="function"><a href="sqlsrv_execute.html" class="function">sqlsrv_execute()</a></span>.
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.sqlsrv-query-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="sqlsrv_prepare.html" class="function" rel="rdfs-seeAlso">sqlsrv_prepare()</a> - Prepares a query for execution</span></li>
    <li class="member"><span class="function"><a href="sqlsrv_execute.html" class="function" rel="rdfs-seeAlso">sqlsrv_execute()</a> - Executes a statement prepared with sqlsrv_prepare</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>